package com.cloud.lan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.lan.api.dto.req.LanManageUserReq;
import com.cloud.lan.api.dto.req.LanUserReq;
import com.cloud.lan.api.dto.resp.LanManageUserResp;
import com.cloud.lan.dao.LanManageUserMapper;
import com.cloud.lan.dao.LanUserMapper;
import com.cloud.lan.entity.base.BaseResponse;
import com.cloud.lan.entity.model.dataobject.LanManageUserDO;
import com.cloud.lan.entity.model.dataobject.LanUserDO;
import com.cloud.lan.entity.utils.VocUtil;
import com.cloud.lan.service.ILanManageUserService;

import org.springframework.stereotype.Service;

import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import lombok.extern.slf4j.Slf4j;

import static com.cloud.lan.entity.utils.ConstantInterface.Enum.ConstantNumber.ONE;

/**
 * <p>
 * 管理系统用户表 服务实现类
 * </p>
 *
 * @author xyqierkang@163.com
 * @since 2021-12-13
 */
@Slf4j
@Service
public class LanManageUserServiceImpl extends ServiceImpl<LanManageUserMapper, LanManageUserDO> implements ILanManageUserService {

    @Resource
    LanUserMapper lanUserMapper;
    @Resource
    private LanManageUserMapper lanManageUserMapper;

    @Override
    public BaseResponse<Object> login(LanManageUserReq request) {
        VocUtil.checkNullAttribute(new String[]{request.getLoginName(), request.getLoginPassword()},
                new String[]{"用户名", "密码"});
        log.info("#查询 用户登录 {}", request);
        LanManageUserResp resp = new LanManageUserResp();
        LanManageUserDO lanManageUserDO = lanManageUserMapper.
                selectOne(new LambdaQueryWrapper<LanManageUserDO>().eq(LanManageUserDO::getLoginName, request.getLoginName()));
        if (ObjectUtil.isEmpty(lanManageUserDO) || !lanManageUserDO.getLoginPassword().equals(SecureUtil.md5(request.getLoginPassword()))) {
            return BaseResponse.error("账号或密码错误！");
        }
        lanManageUserDO.setToken(IdUtil.simpleUUID());
        lanManageUserMapper.updateById(lanManageUserDO);
        BeanUtil.copyProperties(lanManageUserDO, resp);
        return BaseResponse.success(resp);
    }

    @Override
    public BaseResponse<Object> manageList(LanUserReq request) {
        LambdaQueryWrapper<LanUserDO> qw = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(request.getUserPhone())) {
            qw.eq(LanUserDO::getUserPhone, request.getUserPhone());
        }
        if (StrUtil.isNotEmpty(request.getFirstEp())) {
            qw.eq(LanUserDO::getFirstEp, request.getFirstEp());
        }
        if (!request.isCheckAll() && !ONE.getKey().equals(request.getRegisted())) {
            qw.and(q -> q.isNotNull(LanUserDO::getWxName).or()
                    .isNotNull(LanUserDO::getWxHead));
        }
        if (ONE.getKey().equals(request.getRegisted())) {
            qw.isNotNull(LanUserDO::getUserPhone);
        }
        qw.orderByDesc(LanUserDO::getCreateTime);
        Page<LanUserDO> page = new Page<>(request.getPageIndex(), request.getPageSize());
        IPage<LanUserDO> pageList = lanUserMapper.selectPage(page, qw);
        return BaseResponse.success(pageList);
    }

    @Override
    public List<LinkedHashMap<String, Object>> exportUser(String beginDate, String endDate) {
        log.info("#查询 exportUser 用户导出");
        return lanManageUserMapper.exportUser(beginDate, endDate, lanManageUserMapper.exportCount());
    }

}
